package com.qpl.depression.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.common.ServerResponse;
import com.qpl.depression.entity.Question;
import com.qpl.depression.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 跳转到问题解答页面
     *
     * @param modelMap
     * @return
     */
    @GetMapping("question")
    public String questionPage(ModelMap modelMap) {
        List<Question> list = questionService.list(Wrappers.<Question>lambdaQuery().eq(Question::getStatus, 1));
        modelMap.addAttribute("question", list);
        return "question";
    }

    /**
     * 添加问题
     *
     * @param question 问题信息
     * @return 添加结果
     */
    @PostMapping("question/add")
    @ResponseBody
    public ServerResponse questionAdd(Question question) {
        question.setStatus(0);
        return questionService.save(question) ? ServerResponse.createBySuccessMessage("成功") : ServerResponse.createByErrorMessage("失败");
    }

}
